﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace JunQuanPrinter.DB
{
    public struct TDataParam
    {
        public Int32 id;
        public String PRODUCT_CODE;
        public String LINE_NUM;
        public String SPLIT;
        public String PRINT_SPEED;
        public String TIME_DELAY;
        public String WIRE_LEN;
        public String START_POS;
        public String WORD_A;
        public String WORD_B;
        public String WORD_C;
        public String Font;
        public String FontSize;

    }

    class CDataParam
    {
        private static CDataParam m_obj = null;
        private CSQLite m_SQLite = CSQLite.getClass();
        public static CDataParam getClass()
        {
            if(m_obj == null)
            {
                m_obj = new CDataParam();
            }

            return m_obj;
        }


        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public Boolean Add(TDataParam Data)
        {
            String strSQL = @" Insert into tDataParam(PRODUCT_CODE, LINE_NUM, SPLIT, PRINT_SPEED, TIME_DELAY, WIRE_LEN, START_POS, WORD_A, WORD_B, WORD_C, Font, FontSize)
                               Values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}'), '{10}', '{11}') ";
            strSQL = String.Format(strSQL, Data.PRODUCT_CODE, Data.LINE_NUM, Data.SPLIT, Data.PRINT_SPEED, 
                                Data.TIME_DELAY, Data.WIRE_LEN, Data.START_POS, Data.WORD_A, Data.WORD_B, Data.WORD_C, Data.Font, Data.FontSize);
            m_SQLite.ExecuteNonQuery(strSQL);

            return true;  

        }


        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public Boolean Upd(TDataParam Data)
        {
            String strSQL = @" Update tDataParam Set  PRODUCT_CODE = '{0}', LINE_NUM = '{1}', PRINT_SPEED = '{2}', 
                                TIME_DELAY = '{3}', WIRE_LEN = '{4}', START_POS = '{5}',WORD_A = '{6}', WORD_B = '{7}', WORD_C = '{8}', Font = '{9}', Split = '{10}', FontSize = '{11}' where Id = {12}";
            strSQL = String.Format(strSQL, Data.PRODUCT_CODE, Data.LINE_NUM, Data.PRINT_SPEED, Data.TIME_DELAY,
                                    Data.WIRE_LEN, Data.START_POS, Data.WORD_A, Data.WORD_B, Data.WORD_C, Data.Font, Data.SPLIT, Data.FontSize, Data.id);
            m_SQLite.ExecuteNonQuery(strSQL);
            return true;
        }


        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public Boolean AddData(TDataParam Data)
        {
            TDataParam dbData = getData(Data.PRODUCT_CODE, Data.LINE_NUM);
            if(dbData.id == 0)
            {
                Add(Data);
            }
            else
            {
                Data.id = dbData.id;
                Upd(Data);
            }

            return true;
        }

        /// <summary>
        /// 根据行获取结构数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public TDataParam getDataByRow(DataRow dr)
        {
            TDataParam Data = new TDataParam();
            if(dr == null)
            {
                Data.id = 0;
                return Data;
            }
            Data.id = Convert.ToInt32(dr["ID"]);
            Data.PRODUCT_CODE = Convert.ToString(dr["Product_Code"]);
            Data.LINE_NUM = Convert.ToString(dr["Line_Num"]);
            Data.PRINT_SPEED = Convert.ToString(dr["Print_Speed"]);
            Data.SPLIT = Convert.ToString(dr["Split"]);
            Data.TIME_DELAY = Convert.ToString(dr["TIME_DELAY"]);
            Data.WIRE_LEN = Convert.ToString(dr["WIRE_LEN"]);
            Data.START_POS = Convert.ToString(dr["START_POS"]);
            Data.WORD_A = Convert.ToString(dr["WORD_A"]);
            Data.WORD_B = Convert.ToString(dr["WORD_B"]);
            Data.WORD_C = Convert.ToString(dr["WORD_C"]);
            Data.Font = Convert.ToString(dr["Font"]);
            Data.FontSize = Convert.ToString(dr["FontSize"]);       
            return Data;
        }


        /// <summary>
        /// 根据产品卡号及结束代号获取数据
        /// </summary>
        /// <param name="ProductCode"></param>
        /// <param name="LineNum"></param>
        /// <returns></returns>
        public TDataParam getData(String ProductCode, String LineNum)
        {
            String strSQL = @" Select * from tDataParam where Product_Code = '{0}' and Line_Num = '{1}' ";
            strSQL = String.Format(strSQL, ProductCode, LineNum);
            DataTable dt = m_SQLite.ExecuteQueryTable(strSQL);
            if(dt.Rows.Count == 0)
            {
                return getDataByRow(null);
            }

            return getDataByRow(dt.Rows[0]);
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sFilter"></param>
        /// <returns></returns>
        public DataTable getData(String sFilter)
        {
            String strSQL = @" Select * from tDataParam where 1=1 {0}";
            strSQL = String.Format(strSQL, sFilter);
            DataTable dt = m_SQLite.ExecuteQueryTable(strSQL);
            return dt;
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sFilter"></param>
        /// <returns></returns>
        public Boolean DelData(String sFilter)
        {
            String strSQL = @" Delete from tDataParam where 1=1 {0} ";
            strSQL = String.Format(strSQL, sFilter);
            m_SQLite.ExecuteNonQuery(strSQL);
            return true;
        }


        /// <summary>
        /// 获取代号列表
        /// </summary>
        /// <param name="ProductCode"></param>
        /// <returns></returns>
        public String[] getLineNums(String ProductCode)
        {
            String strSQL = @" Select * from tDataParam where Product_Code = '{0}' ";
            strSQL = String.Format(strSQL, ProductCode);
            DataTable dt = m_SQLite.ExecuteQueryTable(strSQL);
            Int32 nCount = dt.Rows.Count;
            String[] sRet = new String[nCount];
            Int32 i = 0;
            foreach(DataRow dr in dt.Rows)
            {
                sRet[i++] = Convert.ToString(dr["LINE_NUM"]);
            }

            return sRet;
        }
    }
}
